package com.cynen.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cynen.pojo.Item;
import com.cynen.service.ItemService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.SQLOutput;
import java.util.Date;
import java.util.List;

@Component
public class Job {

    @Autowired
    private  HttpUtils utils;
    @Autowired
    private ItemService itemService;

    @Scheduled(fixedDelay = 100*1000)
    public void timeJob(){
        // String start = "https://search.jd.com/s_new.php?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&page=7&s=164&click=0";
        String start = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&page=";
        // String url = "https://img10.360buyimg.com/n1/jfs/t1/86026/40/18823/162521/5e999c25E3c8deccc/8edf063ad1ea5e6c.jpg";
        // 查询 5页...每页 55个SPU,每个SPU大概 3个SKU
        for (int i=1;i<=10;i=i+2){
            // 获取页面
            // String url = start+i+"&s=111&click=0";
            String content = utils.doGetHtml(start+i+"&s=1&click=0");
            // System.out.println(content);
            parseHtml(content);
        }
    }

    /**
     * 获取到商品页面,进行解析.
     * @param html
     */
    private void parseHtml(String html){
        // 1.获取文档对象
        Document document = Jsoup.parse(html);
        // 获取spu
        // Element goodsList = document.getElementById("J_goodsList");
        Elements elements = document.select("div#J_goodsList > ul > li");
        for (Element element : elements){
            // 获取SPU
            // System.out.println("SPU:" + element.attr("data-spu"));
            Long spu = Long.parseLong(element.attr("data-spu"));
            // 获取SKU列表
            Elements skus = element.select("li.ps-item");
            for (Element skuEle:skus){
                // 获取SKU对象
                Long sku = Long.parseLong(skuEle.select("[data-sku]").attr("data-sku"));
                // 存储对象:
                Item item = new Item();
                item.setSku(sku);
                // 根据Item查询商品.
                List<Item> items = itemService.findAll(item);
                if(items.size() > 0){
                    // 如果已经保存了,就不用存储,避免重复.
                    continue;
                }
                // spu
                item.setSpu(spu);
                // SKU的图片.
                String picUrl = skuEle.select("img[data-sku]").first().attr("data-lazy-img");
                picUrl = picUrl.replace("/n9/","/n1/");
                String picName = utils.doGetImage("http:"+picUrl);
                item.setPic(picName);

                // SKU的价格:
                String pJson = this.utils.doGetHtml("https://p.3.cn/prices/mgets?skuIds=J_" + sku);
                JSONArray jsonArray = JSON.parseArray(pJson);
                JSONObject jsonObject = (JSONObject) jsonArray.get(0);
                Double price = jsonObject.getDouble("p");
                item.setPrice(price);

                // 商品详情页URL
                String itemUrl = "https://item.jd.com/"+sku+".html";
                item.setUrl(itemUrl);

                // item.setTitle();
                String itemHtml = this.utils.doGetHtml(item.getUrl());
                Document itemDoc = Jsoup.parse(itemHtml);
                String title = itemDoc.select("div.sku-name").first().text();
                item.setTitle(title);

                item.setCreated(new Date());
                item.setUpdated(item.getCreated());

                itemService.save(item);

            }
        }
    }

}
